<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/10/23
 * Time: 8:32
 */
namespace Home\Controller;
use Think\Controller;
use Think\Page;
use Think\Think;

class RecordController extends Controller {
    public function index(){
        $news =M();//通过系统自带的M方法创建表的对象
        $sql="select cid,pnumber from tp_pinformation where pstatus =0";
        $news_list1=$news->query($sql);
        $sql2="select * from tp_price";
        $type=$news->query($sql2);
//        echo $news->getLastSql();
//        var_dump($type);
//        exit();
        $this->assign("type",$type);
        $this->assign("news_list1",$news_list1);
        $news1 =M();
        $count=$news1->count();//获取表中数据的记录总数
        $page=new \Think\Page($count,2);
//        $page->setUrl('url',U());
//        var_dump($page);
//        exit();
       // $page=new \Think\Page($count,2);
        $page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录 第
                    <b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('end','末页');
        $page->setConfig('first','首页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $page->lastSuffix=false;//设置最后一页不显示为总页数
        $show=$page->show();
        $sql="select *,(select uname from tp_user where tp_cost.uid=tp_user.uid) as uname ,(select cnumber from tp_user where tp_cost.uid=tp_user.uid) as cnumber ,(select pnumber from tp_pinformation where tp_pinformation.cid=tp_cost.cid )  as punmber,(select ctype from tp_price where tp_cost.pid=tp_price.pid )  as ctype,(select price from tp_price where tp_cost.pid=tp_price.pid )  as price from tp_cost where outtime is null LIMIT ".$page->firstRow.','.$page->listRows;
        $news_list=$news1->query($sql);
        $news_list=$this->filter($news_list);
//        echo $news->getLastSql();
//       var_dump($news_list);
//        exit();
//            var_dump($show);
//            exit();
        $this->assign('title','简易文件管理系统');
        $this->assign('news_count',$count);
        $this->assign('news_list',$news_list);
        $this->assign('pages',$show);
        $this->display();
    }

    public function leave($id){
        if(session('username')){
            $outtime=time();
            $news =M();
            $sql="select uid, cid, intime,(select price from tp_price where tp_cost.pid=tp_price.pid )  as price from tp_cost where mid='$id' ";
            $data=$news->query($sql);
            foreach ($data as $sk){
                $intime=$sk['intime'];
                $price=$sk['price'];
                $cid=$sk['cid'];
                $uid=$sk['uid'];
            }
            $time=$outtime-$intime;
            $c=$time%86400;
            $ctime=intval($c/3600);
            $money=$ctime*$price;
            $sql="update tp_cost set money='$money', ctime='$ctime',outtime='$outtime' where mid='$id'";
            $news->query($sql);
            $sql1="update tp_pinformation set uid=NULL ,pstatus='0' WHERE  cid='$cid'";
            $news->query($sql1);
            $sql3="update tp_user set cid=null where uid='$uid'";
            $news->query($sql3);
            redirect(U('/Home/Record/index/'));
        }else{
            redirect(U('/Home/Login/index/'),0,'重新登录');
        }
    }

    public function add(){
        $cid=$_POST['n_id'];
        $pid=$_POST['type_id'];
        $idcard=$_POST['idcard'];
        $query=M();
        $sql="select * from tp_user where idcard='$idcard'";
        $data=$query->query($sql);
        foreach ($data as $sk){
            $uid=$sk['uid'];
        }
        if($uid){
            $sql1="update  tp_user set cid='$cid'where uid='$uid'";
            $update=$query->query($sql1);
            $sql2="update tp_pinformation set uid='$uid', pstatus='1' WHERE cid='$cid'";
            $update2=$query->query($sql2);
            $intime=time();
            $sql3="INSERT INTO tp_cost (uid,cid,pid,intime) VALUES ($uid,$cid,$pid,$intime)";
            $insert=$query->query($sql3);
            redirect(U('/Home/Record/index/'));
        }
        else{
        $record = D("User");
        if ($record->create()){
            if ($record->add()){
                $news =M();
                $sql="select MAX(uid) as muid from tp_user ";
                $muid=$news->query($sql);
                foreach ($muid as $sk){
                    $muid=$sk['muid'];
                }
                $sql1="update  tp_user set cid='$cid'where uid='$muid'";
                $update=$news->query($sql1);
                $sql2="update tp_pinformation set uid='$muid', pstatus='1' WHERE cid='$cid'";
                $update2=$news->query($sql2);
                $intime=time();
                $sql3="INSERT INTO tp_cost (uid,cid,pid,intime) VALUES ($muid,$cid,$pid,$intime)";
                $insert=$news->query($sql3);
//                if($update==true && $update2==true  && $insert==true){
//
//                }
//                else{
//                echo $news->getLastSql();
//                exit();
//                    $this->error("登记失败!");
//                }
                redirect(U('/Home/Record/index/'));
            }
       } else {
//            redirect(U('/Admin/Index/index/'),0,'跳转中...');
            redirect(U('/Home/Record/index/'),0,'登记失败');
            }
        }

    }

    private function filter($params){
        foreach ($params as $key=>$value){
            $params[$key]['intime']=date('Y-m-d H:i:s',$value['intime']);
            if (!$value[outtime]){
                $params[$key][outtime]='无';
            }else{
                $params[$key]['outtime']=date('Y-m-d H:i:s',$value['outtime']);
            }
            /*mb_strlen()兼容中文字符  mb_substr()截取字符*/
//            if (mb_strlen($value[subject],'UTF-8')>8){
//                $params[$key]['subject']=mb_substr($value[subject],0,8,'UTF-8').'....';
//            }
        }
        /*$params代表查询的集合*/
        return $params;
    }
}